package cn.fx.basic.service.impl;

import cn.fx.basic.mapper.BaseMapper;
import cn.fx.basic.query.BaseQuery;
import cn.fx.basic.service.IBaseService;
import cn.fx.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
//查询的方法比较多，使用查询的事务配置
//只读，事务传播机制=SUPPORTS，有我就用，没有事务就不用
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    @Transactional
    public void save(T T) {

        baseMapper.save(T);
    }

    @Override
    @Transactional
    public void update(T T) {

        baseMapper.update(T);
    }

    @Override
    @Transactional
    public void remove(long id) {

        baseMapper.remove(id);
    }

    @Override
    public T loadById(Long id) {
        return baseMapper.loadById(id);
    }

    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }

    @Override
    public PageList<T> pageQuery(BaseQuery query) {

        Integer count = baseMapper.queryCount(query);
        if(count < 1){
            return new PageList<T>();
        }

        List<T> list = baseMapper.queryData(query);

        return new PageList<T>(count,list);
    }

    @Override
    @Transactional
    public void patchDel(List<Long> ids) {
        baseMapper.patchDel(ids);
    }

}
